package com.smrs.deliver.webapp.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.jof.framework.util.Pager;
import com.smrs.deliver.enums.DeliverStatusEnum;
import com.smrs.deliver.model.DeliverHeaderModel;
import com.smrs.deliver.model.DeliverHeaderSearchModel;
import com.smrs.basicdata.model.RegionModel;
import com.smrs.basicdata.model.StoreModel;
import com.smrs.goods.model.SupplierModel;
import com.smrs.security.model.UserModel;
import com.smrs.util.AppUtil;



public class DeliverHeaderAction extends BaseDeliverAction{
	private static final long serialVersionUID = 1L;
	private DeliverHeaderModel deliverHeader ;
	
	protected DeliverHeaderSearchModel searchModel;
	protected Pager<DeliverHeaderModel> pager = new Pager<DeliverHeaderModel>(); 
	protected String titleName="入库";
	protected RegionModel region;
	protected StoreModel store;
	protected String buttonTitle;
	protected String deliverHeaderIds;
	
	public String getDeliverHeaderIds() {
		return deliverHeaderIds;
	}

	public void setDeliverHeaderIds(String deliverHeaderIds) {
		this.deliverHeaderIds = deliverHeaderIds;
	}

	public String getButtonTitle() {
		return buttonTitle;
	}

	public void setButtonTitle(String buttonTitle) {
		this.buttonTitle = buttonTitle;
	}

	public DeliverHeaderSearchModel getSearchModel() {
		return searchModel;
	}

	public void setSearchModel(DeliverHeaderSearchModel searchModel) {
		this.searchModel = searchModel;
	}

	public StoreModel getStore() {
		return store;
	}

	public void setStore(StoreModel store) {
		this.store = store;
	}

	public RegionModel getRegion() {
		return region;
	}

	public void setRegion(RegionModel region) {
		this.region = region;
	}

	protected void prepareList(){
		supplierList = supplierService.getAllActive();
		storeList = storeService.getAllActive();
		regionList = regionService.getAllActive();		
	}
	
	public String addDeliverHeader(){
		prepareList();
		
		if(StringUtils.isEmpty(actionCommand)){
			//supplierList = supplierService.getAllActive();
			//storeList = storeService.getAllActive();
			return SUCCESS;
		}
		SupplierModel supplier = this.supplierService.getByPK(deliverHeader.getSupplierId());
		deliverHeader.setSupplierCode(supplier.getCode());
		deliverHeader.setSupplierName(supplier.getName());
		//deliverHeader.setName(supplier.getName());
		store = this.storeService.getByPK(deliverHeader.getStoreId());
		deliverHeader.setStoreCode(store.getCode());
		deliverHeader.setStoreName(store.getName());
		deliverHeader.setSeqNo(deliverSequenceService.updateDeliverSequenceValue());
		deliverHeader.setStatus(DeliverStatusEnum.FRESH.getId());
		region = regionService.getByPK(deliverHeader.getRegion().getId());
		//System.out.println("status="+DeliverStatusEnum.toEnum('F').name());
		
		UserModel userModel = this.getUserModelFromSession();
		AppUtil.setCreateUserInfo(userModel, deliverHeader);
		this.deliverHeaderService.addModel(deliverHeader);		
		this.addActionMessage("创建入库单成功");
		return "toUpdateDeliverHeader";
	}
	
	
	public String searchDeliverHeader(){
		prepareList();
		
		if(searchModel==null){
			searchModel = new DeliverHeaderSearchModel();
		}
		//ActionCommandEnum action = ActionCommandEnum.toEnum(actionCommand);

		pager=deliverHeaderService.findDeliverHeader(searchModel,pager);
		this.actionCommand="query";
		return SUCCESS;
	}
	

	protected void prepareAuditStatus(){
		this.deliverStatusList =new ArrayList<DeliverStatusEnum>();
		//deliverStatusList.add(DeliverStatusEnum.ALL);
		deliverStatusList.add(DeliverStatusEnum.FRESH);
		deliverStatusList.add(DeliverStatusEnum.AUDITED);		
	}
	
	protected void prepareAccountStatus(){
		this.deliverStatusList =new ArrayList<DeliverStatusEnum>();
		//deliverStatusList.add(DeliverStatusEnum.ALL);
		deliverStatusList.add(DeliverStatusEnum.AUDITED);
		deliverStatusList.add(DeliverStatusEnum.ACCOUNTED);		
	}
	
	public String auditDeliverHeader(){
		prepareList();
		prepareAuditStatus();
		if(searchModel==null){
			searchModel = new DeliverHeaderSearchModel();
		}
		pager=deliverHeaderService.findDeliverHeader(searchModel,pager);
		this.actionCommand="audit";
		return SUCCESS;
	}
	
	public String accountDeliverHeader(){
		prepareList();
		prepareAccountStatus();
		if(searchModel==null){
			searchModel = new DeliverHeaderSearchModel();
		}
		pager=deliverHeaderService.findDeliverHeader(searchModel,pager);
		this.actionCommand="account";
		return SUCCESS;
	}
	
	public String addDeliverHeaderInit(){

		return SUCCESS;
	}
	
	public String updateDeliverHeader(){
		this.deliverHeader = deliverHeaderService.getByPK(deliverHeader.getId());
		return SUCCESS;
	}
	
	protected List<Long> parseDeliverHeaderIds(String deliverHeaderIds){
		List<Long> listIds = new ArrayList<Long>();
		String[] ids = deliverHeaderIds.split(",");
		for(int index=0;index<ids.length;index++){
			Long id = Long.parseLong(ids[index]);
			listIds.add(id);
		}
		return listIds;
	}
	
	public String deleteDeliverHeader(){
		deliverHeaderService.deleteModel(deliverHeader);
		return "search";
	}
	
	
	public DeliverHeaderModel getDeliverHeader() {
		return deliverHeader;
	}

	public void setDeliverHeader(DeliverHeaderModel deliverHeader) {
		this.deliverHeader = deliverHeader;
	}
	
	public Pager<DeliverHeaderModel> getPager() {
		return pager;
	}


	public void setPager(Pager<DeliverHeaderModel> pager) {
		this.pager = pager;
	}

	public String getTitleName() {
		return titleName;
	}


	public void setTitleName(String titleName) {
		this.titleName = titleName;
	}
}
